-module(erlsoap_test).
-export([start/0, get_forum_list/2, test/2, get_forum_list_inets/4, get_forum_list_ibrowse/4]).
-include("./../include/JanusAT.hrl").

-define(URL, "http://rsdn.ru/ws/JanusAT.asmx").
-define(PATH, "E:/Mamut/PROJECTS/EclipseWorkspace/Orcas/varia/").


start() ->
    case 
        case soap_registry_server:start(?PATH ++ "soap-envelope.xsd") of
            {ok,_Pid} -> ok;
            {error, {already_started, _Pid}} -> ok;
            OtherErr -> OtherErr
        end 
        of
            ok -> soap_registry_server:add_xsd(?URL, ?PATH ++ "JanusAT.xsd");
            Other -> Other
        end.

get_forum_list(User,Pass) ->
    get_forum_list_inets(User, Pass, [], []).

get_forum_list_inets(User,Pass, Options, Headers) ->
    case soapclient:invoke(?URL,
                           "\"http://rsdn.ru/Janus/GetForumList\"",
                           #'p:GetForumList'{'forumRequest'=#'p:ForumRequest'{userName=User, password=Pass}}, 
                           {inets, Options, Headers}) of
        {ok, Reply} -> Reply;
        {error, Error} -> {error, Error}
	end.

get_forum_list_ibrowse(User,Pass, Options, Headers) ->
    case soapclient:invoke(?URL,
                           "\"http://rsdn.ru/Janus/GetForumList\"",
                           #'p:GetForumList'{'forumRequest'=#'p:ForumRequest'{userName=User, password=Pass}}, 
                           {ibrowse, Options, Headers}) of
        {ok, Reply} -> Reply;
        {error, Error} -> {error, Error}
	end.

test(UserName, Password) ->
    start(),
    io:format("Sending request via inets~n"),    
    Inets = get_forum_list_inets(UserName, Password, [], []),
    io:format("Sending request via ibrowse~n"),    
    Ibrowse = get_forum_list_ibrowse(UserName, Password, [], []),
    io:format("Are their results identical? ~p~n", [Inets == Ibrowse]).

